// pages/books/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        categories: ['历史', '小说', '文学', '艺术', '哲学', '经济', '心理', '社科'],
        activeIndex: 0,
        bookList: [],
        bookCoverUrl: 'https://ov-library-1316830564.cos.ap-guangzhou.myqcloud.com/bookCover%2F',
        defaultCover: '/resource/images/defaultCover.png'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.fetchBooksByCategory(this.data.categories[0]);
    },

    onTabClick(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            activeIndex: index
        });
        this.fetchBooksByCategory(this.data.categories[index]);
    },

    fetchBooksByCategory(categoryName) {
        wx.request({
            url: 'http://localhost:8080/books/queryByCategory',
            method: 'GET',
            data: {
                category: categoryName
            },
            success: res => {
                this.setData({
                    bookList: res.data.data
                });
            },
            fail: () => {
                wx.showToast({
                    title: '获取书籍失败',
                    icon: 'none'
                });
            }
        });
    },

    onClickSearch() {
        wx.navigateTo({
            url: '/pages/books/search/search',
        })
    },

    goToBookDetail(event) {
        console.log("event:" + event)
        const bookId = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/books/detailed/detailed?id=${bookId}`
        });
    },

    // onPullDownRefresh() {
    //     console.log("12345")
    //     wx.request({
    //         url: 'http://localhost:8080/books/queryByCategory',
    //         method: 'GET',
    //         data: {
    //             category: categoryName,
    //             page: 2
    //         },
    //         success: res => {
    //             this.setData({
    //                 bookList: res.data.data
    //             });
    //         },
    //         fail: () => {
    //             wx.showToast({
    //                 title: '获取书籍失败',
    //                 icon: 'none'
    //             });
    //         }
    //     });
    // },
})